package win.smartown.app.gank.day.data;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.HashMap;

import win.smartown.app.R;
import win.smartown.app.base.LoadingActivity;
import win.smartown.app.base.SuperActivity;
import win.smartown.app.common.WebActivity;
import win.smartown.app.gank.entity.Data;
import win.smartown.app.util.JumpUtil;
import win.smartown.easyAdapter.EasyAdapter;
import win.smartown.easyAdapter.EasyViewHolder;

/**
 * Created by smartown on 2017/5/5 15:14.
 * Desc:每日干货
 */
public class DayDataActivity extends LoadingActivity implements Contract.View, View.OnClickListener {

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    private DayDataPresenter presenter;
    private RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContent(R.layout.activity_type_data);
        init();

        swipeRefreshLayout = findView(R.id.swipe_refresh);
        recyclerView = findView(R.id.recycler_view);
        recyclerView.setBackgroundColor(Color.WHITE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getDayData(2017, 5, 5);
            }
        });

        presenter.getDayData(2017, 5, 5);
    }

    private void init() {
        presenter = new DayDataPresenter(this);
        adapter = new EasyAdapter(this) {

            @Override
            public int getItemViewType(int position) {
                return presenter.getDataList().get(position).getType();
            }

            @Override
            public int getItemLayout(int viewType) {
                if (viewType == 1) {
                    return R.layout.item_day_data_type;
                }
                return R.layout.item_day_data;
            }

            @Override
            public void onBindViewHolder(EasyViewHolder holder, int position) {
                switch (getItemViewType(position)) {
                    case 1:
                        final String type = (String) presenter.getDataList().get(position).getObject();
                        holder.getTextView(R.id.type).setText(type);
                        break;
                    case 2:
                        final Data data = (Data) presenter.getDataList().get(position).getObject();
                        holder.getTextView(R.id.data_desc).setText(data.getDesc());
                        holder.getTextView(R.id.data_who).setText(data.getWho());
                        holder.itemView.setTag(data);
                        holder.itemView.setOnClickListener(DayDataActivity.this);
                        break;
                }
            }

            @Override
            public int getItemCount() {
                return presenter.getDataList().size();
            }
        };
    }

    @Override
    public SuperActivity getSuperActivity() {
        return this;
    }

    @Override
    public void finishLoading() {
        super.finishLoading();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onDataChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (v.getTag() instanceof Data) {
            final Data data = (Data) v.getTag();
            HashMap<String, String> map = new HashMap<>();
            map.put("url", data.getUrl());
            JumpUtil.startActivity(this, WebActivity.class, data.getDesc(), map);
        }
    }
}
